import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const state = {
    http: 'http://suzuki-dev-finance.haimaiche.net:8999', //http://suzuki-dev-finance.haimaiche.net:8999
    side: sessionStorage.side ? sessionStorage.side : 'order', //侧边栏当前位置
    token: '', //当前账号token
    power: '', //当前账号角色
    saveNum: 0, //保存动作中当前的保存数量
    saveNumAll: 0, //保存动作中总的保存数量
    staffId: 123,
    staffName: '',
    isAllMust: false, //是否全部完成
    isSubmit: false, //是否是提交
    otherData: {}, //贷款申请其它资料
    msgCount: '', //消息条数
    tip: {
        title: '',
        theme: '',
        enter: '',
        status: 0,
    }, //提醒
    bank: [],
    bankOpp: [],
    dealer: [], //经销商
    opearDepart: [], //业务部门
    sex: [
        { label: '男', value: 0 },
        { label: '女', value: 1 },
    ], //性别
    marryStatus: [
        { label: '已婚有子女', value: 0 },
        { label: '已婚无子女', value: 1 },
        { label: '未婚单身', value: 2 },
        { label: '离异单身', value: 3 },
        { label: '丧偶单身', value: 4 },
    ], //婚姻状态
    serviceSource: [], //业务来源
    education: [], //学历
    unitType: [], //单位类型
    job: [], //职位
    seniority: [], //工龄
    creditYear: [], //贷款年限
    link: [], //与购车人关系
    linkOther: [], //与购车人关系
    houseType: [], //房产类型
    orderStatus: [], //订单状态
    orderStatusLoan: [], //订单状态-贷款申请列表
    gpsFactory: [], //gps厂商
    carOriginType: [], //车辆产地性质
    // carBrand: [], //品牌车型
    seating: [], //车辆座位数
    barVehicleComposition: [], //车型构成
    carRenewalInsurance: [
        { label: '有', value: true },
        { label: '无', value: false }
    ], //车辆有无续保
    serviceType: [], //业务类型
    isSuccess: [
        { label: '是', value: true },
        { label: '否', value: false }
    ], //是否办理成功,也可适用其它是否选项
    courierCompany: [], //快递公司
    attachment: [
        { label: '购车人和共同购车人', value: 0 },
        { label: '担保人', value: 1 },
        { label: '资产保全部', value: 2 },
        { label: '财务部', value: 3 },
        { label: '车辆附件', value: 4 },
        { label: '其它信息录入附件', value: 5 }
    ], //附件类型
    checkStatus: [
        { text: '贷款申请', value: 1 },
        { text: '通融申请', value: 3 },
        { text: '修改申请', value: 4 },
    ], //审批类型
    invaildReason: [], //作废原因
}

if (!state.token) {
    state.token = sessionStorage.getItem('token');
}
if (!state.power) {
    state.power = sessionStorage.getItem('power');
}

const mutations = {
    bankOpp(state, _this) {
        _this.this.$libs.getDictionary(state, _this, {
            url: '/dataDict/bank',
            key: 'bankOpp',
            label: 'dataId',
            value: 'name',
            cb: _this.cb ? _this.cb() : ''
        });
    },
    bank(state, _this) {
        _this.$libs.getDictionary(state, _this, {
            url: '/dataDict/bank',
            key: 'bank',
            label: 'name',
            value: 'dataId'
        });
    },
    seating(state, _this) {
        _this.$libs.getDictionary(state, _this, {
            url: '/dataDict/seating',
            key: 'seating',
            label: 'name',
            value: 'code'
        });
    },
    invaildReason(state, _this) {
        _this.$libs.getDictionary(state, _this, {
            url: '/dataDict/invaildReason',
            key: 'invaildReason',
            label: 'name',
            value: 'code'
        });
    },
    creditYear(state, _this) {
        _this.$libs.getDictionary(state, _this, {
            url: '/dataDict/loanPeriod',
            key: 'creditYear',
            label: 'name',
            value: 'code'
        });
    },
    seniority(state, _this) {
        _this.$libs.getDictionary(state, _this, {
            url: '/dataDict/workingAge',
            key: 'seniority',
            label: 'name',
            value: 'code'
        });
    },
    getUnreadCount(state, _this) {
        _this.$http.get("/messages/count")
            .then((respnse) => {
                console.log(respnse)
                state.msgCount = respnse.data.data
            })
            .catch(function(error) {
                console.log(error)
            })
    },
    unitType(state, _this) {
        _this.$libs.getDictionary(state, _this, {
            url: '/dataDict/dealerType',
            key: 'unitType',
            label: 'name',
            value: 'code'
        });
    },
    houseType(state, _this) {
        _this.$libs.getDictionary(state, _this, {
            url: '/dataDict/roomType',
            key: 'houseType',
            label: 'name',
            value: 'code'
        });
    },
    job(state, _this) {
        _this.$libs.getDictionary(state, _this, {
            url: '/dataDict/duty',
            key: 'job',
            label: 'name',
            value: 'code'
        });
    },
    education(state, _this) {
        _this.$libs.getDictionary(state, _this, {
            url: '/dataDict/education',
            key: 'education',
            label: 'name',
            value: 'code'
        });
    },
    linkOther(state, _this) {
        _this.$libs.getDictionary(state, _this, {
            url: '/dataDict/kinship',
            key: 'linkOther',
            label: 'name',
            value: 'code'
        });
    },
    link(state, _this) {
        _this.$libs.getDictionary(state, _this, {
            url: '/dataDict/ownRel',
            key: 'link',
            label: 'name',
            value: 'code'
        });
    },
    courierCompany(state, _this) {
        _this.$libs.getDictionary(state, _this, {
            url: '/dataDict/expressCompany',
            key: 'courierCompany',
            label: 'name',
            value: 'code'
        });
    },
    serviceSource(state, _this) {
        _this.$libs.getDictionary(state, _this, {
            url: '/dataDict/bizOrigin',
            key: 'serviceSource',
            label: 'name',
            value: 'code'
        });
    },
    carOriginType(state, _this) {
        _this.$libs.getDictionary(state, _this, {
            url: '/dataDict/vehicleOrigin',
            key: 'carOriginType',
            label: 'name',
            value: 'code'
        });
    },
    barVehicleComposition(state, _this) {
        _this.$libs.getDictionary(state, _this, {
            url: '/dataDict/carType',
            key: 'barVehicleComposition',
            label: 'name',
            value: 'code'
        });
    },
    serviceType(state, _this) {
        _this.$libs.getDictionary(state, _this, {
            url: '/dataDict/businessType',
            key: 'serviceType',
            label: 'name',
            value: 'code'
        });
    },
    gpsFactory(state, _this) {
        _this.$libs.getDictionary(state, _this, {
            url: '/dataDict/gpsFactory',
            key: 'gpsFactory',
            label: 'name',
            value: 'code'
        });
    },
    updataOpearDepart(state, _this) {
        for (let i = 0; i < state.opearDepart.length; i++) {
            let value = state.opearDepart[i];
            if (_this.val[0] == value.label) {
                _this.this.$http.get('/audit/getDeptList?parentDeptId=' + value.id).then((data) => {
                    if (data.data.success) {
                        for (let j = 0; j < data.data.data.length; j++) {
                            let datas = data.data.data[j];
                            state.opearDepart[i].cities.push({
                                label: datas.deptName,
                                id: datas.deptId,
                            });
                        }
                    }
                }).catch((error) => {

                });
            }
        }
    },
    opearDepart(state, _this) {
        _this.$http.get('/audit/getDeptList').then((data) => {
            if (data.data.success) {
                state.opearDepart = [];
                for (var i = 0; i < data.data.data.length; i++) {
                    let el = data.data.data[i];
                    state.opearDepart.push({
                        label: el.deptName,
                        id: el.deptId,
                        cities: []
                    });
                }
            }
        }).catch((error) => {

        });
    },
    dealer(state, _this) {
        _this.this.$libs.getDictionary(state, _this, {
            url: '/dealer/lists/' + _this.key,
            key: 'dealer',
            label: 'dealerId',
            value: 'dealerName',
            cb: _this.cb ? _this.cb() : ''
        });
    },
    orderStatus(state, _this) {
        _this.$libs.getDictionary(state, _this, {
            url: '/commonData/orderStatus/',
            key: 'orderStatus',
            label: 'msg',
            value: 'code'
        });
    },
    orderStatusLoan(state, _this) {
        _this.$libs.getDictionary(state, _this, {
            url: '/commonData/orderStatus/',
            key: 'orderStatusLoan',
            label: 'msg',
            value: 'code',
            filter: [10, 11, 12, 13]
        });
    },
    loanPeriod(state, _this) {
        _this.$libs.getDictionary(state, _this, {
            url: '/dataDict/loanPeriod',
            key: 'loanPeriod',
            label: 'name',
            value: 'code'
        });
    },
    sideChange(state, _side) {
        state.side = _side;
        sessionStorage.setItem('side', _side);
    },
    isAllMustFalse(state) {
        state.isAllMust = true;
    },
    isAllMustTrue(state) {
        state.isAllMust = false;
    },
    saveNumClear(state, saveNumAll) {
        state.saveNum = 0;
        state.saveNumAll = saveNumAll;
    },
    toSubmit(state) {
        state.isSubmit = true;
    },
    toSubmitFalse(state) {
        state.isSubmit = false;
    },
    clearStatus(state) {
        state.isSubmit = false;
        state.isAllMust = false;
    },
    tipChange(state, _tip) {
        state.tip = {
            title: _tip.title,
            theme: _tip.theme,
            enter: _tip.enter,
            status: _tip.status ? _tip.status : 0,
            cb: _tip.cb ? _tip.cb : '',
            param: _tip.param
        }
    },
    saveNumAdd(state, _this, _id, _other) {
        if (_this.otherDate) {
            state.otherData = _this.otherDate;
        }
        state.saveNum++;
        console.log(_this);
        if (!state.isSubmit && state.saveNum == state.saveNumAll) {
            // 保存成功
            if (state.tip.status == 0) {
                _this.$libs.successAlert(_this, state.tip);
            } else if (state.tip.status == 1) {
                // 修改流程发起
                let param = {
                    applyType: 4,
                    orderNo: _this.id ? _this.id : '',
                    remark: "修改申请",
                }
                _this.$http.post('/submitTaskController/updateApply', param).then((data) => {
                    if (data.data.success) {
                        _this.$libs.successAlert(_this, state.tip);
                    } else {
                        if (data.data.message == '订单状态无效，无法发起申请') {
                            _this.$confirm('征信结果未录入，无法进行提交！', '取消', {
                                confirmButtonText: '确定',
                                cancelButtonText: '取消',
                                type: 'warning'
                            }).then(() => {
                                _this.$router.push({ path: '/order' });
                            }).catch((error) => {});
                        } else {
                            _this.$message.error(data.data.message);
                        }
                    }
                }).catch((error) => {

                });
                state.tip.status = 0;
            }
        } else if (state.isSubmit && state.saveNum == state.saveNumAll) {
            if (state.tip.status == 1 && state.tip.param) {
                // 通融申请提交
                _this.$http.post('/submitTaskController/accomApply', state.tip.param).then((data) => {
                    if (data.data.success) {
                        _this.$libs.successAlert(_this, state.tip, () => {
                            _this.$router.push({ path: '/order' })
                        });
                    } else {
                        if (data.data.message == '订单状态无效，无法发起申请') {
                            _this.$confirm('征信结果未录入，无法进行提交！', '取消', {
                                confirmButtonText: '确定',
                                cancelButtonText: '取消',
                                type: 'warning'
                            }).then(() => {
                                _this.$router.push({ path: '/order' });
                            }).catch((error) => {});
                        } else {
                            _this.$message.error(data.data.message);
                        }
                    }
                }).catch((error) => {

                });
                mutations.isAllMustTrue();
            } else {
                // 贷款申请提交
                let param = {
                    applyType: 1,
                    bizType: state.otherData.bizType,
                    loanMoney: state.otherData.loanMoney,
                    orderNo: _this.id ? _this.id : '',
                    remark: "贷款申请",
                };
                _this.$http.post('/submitTaskController/salesManLoanApply', param).then((data) => {
                    if (data.data.success) {
                        _this.$libs.successAlert(_this, state.tip, () => {
                            _this.$router.push({ path: '/order' })
                        });
                    } else {
                        _this.$message.error(data.data.message);
                    }
                }).catch((error) => {

                });
                mutations.isAllMustTrue();
            }
        }
    }
}

const actions = {}

const getters = {

}


export default new Vuex.Store({
    state,
    mutations,
    actions,
    getters
})
